package com.genokon.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.genokon.dto.OmiDiseaseDto;
import com.genokon.entry.GeneHpoEntry;
import com.genokon.entry.GeneOmiEntry;
import com.genokon.mapper.HpoMapper;
import com.genokon.mapper.OmiMapper;
import com.genokon.service.HpoAndOmiService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @author by baichaoda
 * @date 2020/11/6 13:50
 */
@Service("hpoAndOmiService")
public class HpoAndOmiServiceImpl implements HpoAndOmiService {
    @Resource
    private HpoMapper hpoMapper;
    @Resource
    private OmiMapper omiMapper;

    @Override
    public GeneHpoEntry getByHpoId(String hpoId) {
        return hpoMapper.selectOne(new QueryWrapper<GeneHpoEntry>().lambda().eq(GeneHpoEntry::getHpoId, hpoId));
    }

    @Override
    public GeneOmiEntry getByOmiNumber(String omiNo) {
        Map<String, Object> con = new HashMap<>(1);
        con.put("mim_id", omiNo);
        con.put("type", 1);
        return omiMapper.selectOne(new QueryWrapper<GeneOmiEntry>().allEq(con));
    }

    @Override
    public OmiDiseaseDto getSymptomByMinId(Integer minId) {
        if(minId == null){
            return null;
        }
        OmiDiseaseDto dto = omiMapper.findOmiSymptomByMinId(minId);
        if (ObjectUtils.isEmpty(dto)){
            return null;
        }
        int pos = dto.getPhenotype().indexOf(";");
        if (pos != -1){
            dto.setPhenotype(dto.getPhenotype().substring(0, pos -1));
        }
        //如果CHPO没有内容，则取基源翻译
        if (StringUtils.isEmpty(dto.getPhenotypeCn())){
            dto.setPhenotypeCn(dto.getTitle());
        }

        if (!StringUtils.isEmpty(dto.getPhenotypeCn())){
            dto.setPhenotype(dto.getPhenotype() + "（" + dto.getPhenotypeCn() + "）");
        }

        if (!CollectionUtils.isEmpty(dto.getClinicalSynopsis())){
            dto.getClinicalSynopsis().forEach(item->{
                if (!CollectionUtils.isEmpty(item.getData())){
                    item.getData().forEach(it->{
                        if (!CollectionUtils.isEmpty(it.getSymptoms())){
                            it.setDetail(String.join("，", it.getSymptoms().toArray(new String[0])));
                        }
                    });
                }
            });
        }
        return dto;
    }
}
